package main

import (
	"crypto/hmac"
	"crypto/sha256"
	"encoding/hex"
	"fmt"
)

func main() {
	mac1 := generate_hmac1([]byte("王彦超天才"), []byte("1212"))
	fmt.Println("mac1:", mac1)
	fmt.Println("mac1:", mac1[:])
	fmt.Println("mac1:", string(mac1[:]))
	fmt.Println("mac1:", fmt.Sprintf("%x", mac1))
	fmt.Println("mac1:", fmt.Sprintf("%x", mac1[:]))
	fmt.Println("mac1:", hex.EncodeToString(mac1))
	mac1_str:= hex.EncodeToString(mac1);
	byte_test:=[]byte(mac1_str);
	fmt.Println("byte_test:",byte_test)
	fmt.Println("byte_test_string:",string(byte_test))
	fmt.Println(`---------------------------------------------------`)
	certificationHmac([]byte("王彦超天才"), []byte("1212"), mac1)
}

/*
	生成hmac mac1
*/
func generate_hmac1(src, key []byte) []byte {
	// 1.生成hasher 控制类
	hasher := hmac.New(sha256.New, key)
	// 2.给hash控制类传入内容
	hasher.Write(src)
	// 3.生成hash mac
	mac1 := hasher.Sum(nil)
	fmt.Println("生成mac1成功！")
	return mac1
}

/*
	认证 传入数据 ，传入key 传入mac1是否正确
*/
func certificationHmac(src, key, mac1 []byte) {
	// 1.得到内容
	// 2.得到mac1
	// 3.根据得到的src和key计算本地的mac2
	mac2 := generate_hmac1(src, key)
	fmt.Println("mac2:", hex.EncodeToString(mac2))
	// 4.对比mac1 mac2
	isok := hmac.Equal(mac1, mac2)
	fmt.Println("isok：", isok)
}
